package com.mt.demo.manager;

import com.mt.demo.domain.MtMember;
import com.mt.demo.domain.MtMemberDetail;
import com.mt.demo.service.IMtMemberDetailService;
import com.mt.demo.service.IMtMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Component
public class MeetingUserManager {
    @Autowired
    private IMtMemberService memberService;

    @Autowired
    private IMtMemberDetailService memberDetailService;

    // 会议ID -> 在线用户列表
    private final Map<String, Map<String, MtMember>> meetingOnlineUsers = new ConcurrentHashMap<>();

    // 添加在线用户
    public void addOnlineUser(String meetingId, MtMember member) {
        meetingOnlineUsers.computeIfAbsent(meetingId, k -> new ConcurrentHashMap<>())
                .put(member.getUserId().toString(), member);
    }

    // 移除在线用户
    public void removeOnlineUser(String meetingId, String userId) {
        Map<String, MtMember> users = meetingOnlineUsers.get(meetingId);
        if (users != null) {
            MtMember member = users.remove(userId);
            if (member != null) {
                // 更新用户退出时间
                updateUserLeaveTime(meetingId, userId);
            }
            if (users.isEmpty()) {
                meetingOnlineUsers.remove(meetingId);
            }
        }
    }

    // 获取会议在线用户列表
    public List<MtMember> getOnlineUsers(String meetingId) {
        Map<String, MtMember> users = meetingOnlineUsers.get(meetingId);
        return users != null ? users.values().stream().collect(Collectors.toList()) : new ArrayList<>();
    }

    // 更新用户退出时间
    private void updateUserLeaveTime(String meetingId, String userId) {
        // 更新会议成员表
        MtMember member = new MtMember();
        member.setUserId(Long.parseLong(userId));
        member.setMeetingId(Long.parseLong(meetingId));
        member.setLastLeave(new Date());
        memberService.updateMtMember(member);

        // 更新会议成员详情表
        MtMemberDetail detail = new MtMemberDetail();
        detail.setUserId(Long.parseLong(userId));
        detail.setMeetingId(Long.parseLong(meetingId));
        detail.setLeaveTime(new Date());
        memberDetailService.updateMtMemberDetail(detail);
    }

    // 检查用户是否在线
    public boolean isUserOnline(String meetingId, String userId) {
        Map<String, MtMember> users = meetingOnlineUsers.get(meetingId);
        return users != null && users.containsKey(userId);
    }

    // 获取会议在线用户数
    public int getOnlineUserCount(String meetingId) {
        Map<String, MtMember> users = meetingOnlineUsers.get(meetingId);
        return users != null ? users.size() : 0;
    }

    // 同步会议用户状态
    public void syncMeetingUsers(String meetingId, List<String> connectedUserIds) {
        Map<String, MtMember> onlineUsers = meetingOnlineUsers.computeIfAbsent(meetingId, k -> new ConcurrentHashMap<>());
        
        // 移除不在连接列表中的用户
        onlineUsers.keySet().removeIf(userId -> !connectedUserIds.contains(userId));

        // 添加新连接的用户
        for (String userId : connectedUserIds) {
            if (!onlineUsers.containsKey(userId)) {
                try {
                    MtMember member = memberService.selectMtMemberByUserId(Long.parseLong(userId));
                    if (member != null) {
                        onlineUsers.put(userId, member);
                    }
                } catch (NumberFormatException e) {
                    // 忽略无效的用户ID
                }
            }
        }
    }
}
